﻿<%@ WebHandler Language="C#" Class="AnswerHandler" %>

using System;
using System.Collections.Generic;
using System.Web;

using EHSubmit.Util;
using EHSubmit.Logic;
using EHSubmit.Logic.LINQ;
using EHSubmit.Model;

public class AnswerHandler : EHSubmit.Logic.BaseAjaxHandler {

    protected override void Mapping()
    {
        OprationMap["addObjectiveWork"] = "AddObjectiveWork";                    //列出习题
    }

    public void AddObjectiveWork()
    {
        try
        {
            int goalId = int.Parse(Context.Request["goalId"]);
            
            QuestionManager quesManager = new QuestionManager();
            IList<Question> ques = quesManager.GetQuestionsWithoutOptions(goalId);
            
            // 此次答题的答案列表
            IList<Answer> ansers = new List<Answer>();
            
            
            int total = ques.Count;
            float rightCount = 0;
            string currAnswer = null;
            foreach(Question q in ques){
                currAnswer = Context.Request[q.QID.ToString()];

                // 允许某些题不做，系统将算作错误
                if (currAnswer == null)
                {
                    currAnswer = string.Empty;
                }
                
                Answer an = new Answer();
                an.OwnerAnswer = currAnswer;     //格式：多选答案ab，填空答案a,b
                an.IsRight = false;
                an.OwnerID = CurrentUser.ID;  
                an.QuestionID = q.QID;
                
                switch (q.Type){
                    case 0:        //单选
                    case 1:        //多选
                    case 2:        //判断
                        {
                            currAnswer = currAnswer.Replace(",", string.Empty);
                            
                            if (q.Answer == currAnswer)
                            {
                                an.IsRight = true;
                                rightCount++;
                            }
                            else
                            {
                                an.IsRight = false;
                            }
                            
                            break;
                        }
                    case 3:        //填空
                        {
                            
                            IList<string> currBlankAns = currAnswer.Split(',');
                            IList<string> realBlankAns = q.Answer.Split(',');
                            IList<int> blankAnsRightCompared = new List<int>();

                            if (currBlankAns.Count == realBlankAns.Count)    //如果学生答案的数量与实际答案的数量不符就算错
                            {
                                foreach (string cba in currBlankAns)     //a,a 对应 a,b则会有问题    a,b,c ---a,b      答案与顺序无关
                                {
                                    foreach (string rba in realBlankAns)
                                    {
                                        if (blankAnsRightCompared.Contains(rba.GetHashCode()) == false)       //判断对的是否已比较
                                        {
                                            if (cba.Trim() == rba.Trim())
                                            {
                                                blankAnsRightCompared.Add(rba.GetHashCode());
                                                break;
                                            }
                                        }
                                    }
                                }
                                
                                //总共匹配的数量与实际答案一致才算对
                                if (blankAnsRightCompared.Count == realBlankAns.Count)
                                {
                                    an.IsRight = true;
                                    rightCount++;
                                }
                                else
                                {
                                    an.IsRight = false;
                                }
                            }
                            //如果学生答案的数量与实际答案的数量不符就算错
                            else
                                an.IsRight = false;
                            
                            break;
                        }
                }

                
                // 添加自己的答案
                ansers.Add(an);
            }

            //------------添加用户的答案，留作分析
            AnswerManager anserManager = new AnswerManager();
            anserManager.AddAnswers(ansers, goalId);
            

            //计算成绩
            int score = Convert.ToInt32(rightCount / total * 100);
            
            
            //-------------插入作业信息
            HomeworkManager hmManager = new HomeworkManager();
            Homework hmwork = new Homework();

            //hmwork.Description = Context.Request["description"];
            //hmwork.AttachPath = hFileName;         // 这里不将物理地址存入库,在列表时要通过JSON读AttachPath字段,使用物理地址可能不安全,将虚拟地址入库
            hmwork.GoalName = Context.Request["goalName"];
            hmwork.GoalID = goalId;
            hmwork.GoalType = 1;  // 客观题
            hmwork.StudentID = CurrentUser.ID;
            hmwork.SubmitTime = DateTime.Now;
            hmwork.UpdateTime = hmwork.SubmitTime;
            hmwork.Term = ConfigHelper.GetTerm();   // TODO   暂时根据配置文件获得当前学期，本应根据教务处同步，待处理。
            hmwork.Scores = score;
            
            hmManager.AddHomework(hmwork);

                        
            
            //-----------服务器应答
            IsSuccess = true;
            ResponseText = "{" + string.Format("success:true,msg:'答题结束，您的得分是：{0}'", score) + "}";
            
        }
        catch (Exception e)
        {
            LogHelper.Log(e.Message, e);
            Message = e.Message;
        }
    }


}